package org.example.sunrise.sprites;

import javafx.scene.image.Image;
import lombok.Getter;
import lombok.NoArgsConstructor;
import lombok.Setter;
import org.example.sunrise.utils.ImageUtil;

import java.util.*;
import java.util.function.Consumer;

@Getter
@Setter
@NoArgsConstructor
public class Sprite {
    private static final Map<String, Image> cache = new HashMap<>();
    private double positionX;
    private double positionY;
    private double velocityX;
    private double velocityY;
    private double width;
    private double height;
    private int row;
    private Image image;
    private boolean needDestroy;
    private final List<Consumer<Sprite>> onClickListeners = new ArrayList<>();

    public Sprite(String path) {
        this(path, 0, 0);
    }

    public Sprite(String path, double width, double height) {
        this(ImageUtil.createImage(path, width, height, Sprite.class));
    }

    public Sprite(Image image) {
        setImage(image);
    }

    public void switchImage() {
    }

    public void update() {
    }

    public void release() {
    }

    public void onMouseClicked() {
        onClickListeners.forEach(listener -> listener.accept(this));
    }

    public void addOnClickListener(Consumer<Sprite> listener) {
        onClickListeners.add(listener);
    }

    public void setImage(Image image) {
        this.image = image;
        setHeight(image.getHeight());
        setWidth(image.getWidth());
    }

    public boolean isChosen(double x, double y) {
        return x >= positionX && x <= positionX + width && y >= positionY && y <= positionY + height;
    }

    public boolean isNeedDestroy() {
        if (needDestroy) {
            release();
        }
        return needDestroy;
    }
}
